package com.wind.ipagent.service;

import com.wind.ipagent.kdl.Auth;
import com.wind.ipagent.kdl.Client;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;


@Service
public class KdlService {

    @Value("${dps.secretId}")
    private String dpsSecretId;

    @Value("${dps.secretKey}")
    private String dpsSecretKey;

    private Client dpsClient;


    @Value("${kps.secretId}")
    private String kpsSecretId;

    @Value("${kps.secretKey}")
    private String kpsSecretKey;

    private Client kpsClient;

    public Client getDpsClient() {
        return dpsClient;
    }

    public Client getKpsClient() {
        return kpsClient;
    }

    @PostConstruct
    public void initClient(){
        if(StringUtils.isNotEmpty(dpsSecretId)&&StringUtils.isNotEmpty(dpsSecretKey)){
            Auth auth = new Auth(dpsSecretId, dpsSecretKey);
            dpsClient = new Client(auth);
        }
        if(StringUtils.isNotEmpty(kpsSecretId)&&StringUtils.isNotEmpty(kpsSecretKey)){
            Auth auth = new Auth(kpsSecretId, kpsSecretKey);
            kpsClient = new Client(auth);
        }



    }

    public String[] getDps(int num) throws Exception {
        return  dpsClient.get_dps(num);
    }

    public String[] getDpsIpWhitelist() throws Exception {

        return  dpsClient.get_ip_whitelist();
    }


    public void setDpsIpWhitelist(String iplist) throws Exception {
            dpsClient.set_ip_whitelist(iplist);
    }

    public void setDpsIpWhitelist(String[] iplist) throws Exception {
        dpsClient.set_ip_whitelist(iplist);

    }

    public Map<String, Integer> getDpsValidTime(String proxy) throws Exception {
        return  dpsClient.get_dps_valid_time(proxy);
    }

    public Map<String, Integer> getDpsValidTime(String[] proxy) throws Exception {
        return  dpsClient.get_dps_valid_time(proxy);
    }


    // kps

    public String[] getKps(int num) throws Exception {
        return  kpsClient.get_kps(num);
    }

    public String[] getKpsIpWhitelist() throws Exception {

        return  kpsClient.get_ip_whitelist();
    }


    public void setKpsIpWhitelist(String iplist) throws Exception {
        kpsClient.set_ip_whitelist(iplist);

    }

    public void setKpsIpWhitelist(String[] iplist) throws Exception {
        kpsClient.set_ip_whitelist(iplist);

    }


}
